package cn.tedu.stage2.thread;

/*
 * 静态方法上使用synchronized后，那么该方法一定具有同步效果。
 */
public class SyncDemo3 {
    public static void main(String[] args) {
        Boo b1 = new Boo();
        Boo b2 = new Boo();
        Boo b3 = new Boo();
        Boo b4 = new Boo();
        Thread t1 = new Thread(){
            public void run(){
                b1.dosome();
            }
        };

        Thread t2 = new Thread() {
            public void run() {
                b2.dosome();
            }
        };
        Thread t3 = new Thread(){
            public void run(){
                b3.dosome();
            }
        };

        Thread t4 = new Thread() {
            public void run() {
                b4.dosome();
            }
        };

        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }

}

class Boo{
    public static void dosome(){
        synchronized (Boo.class) {
            try {
                Thread t = Thread.currentThread();
                System.out.println(t.getName() + ":正在执行dosome方法...");
                Thread.sleep(1000);
                System.out.println(t.getName()+":执行dosome方法完毕！");
            } catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }


}
